package com.zang.blogz.mail;


import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;


import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Map;

/**
 * @Author: 橘贝贝
 * @Description:
 * @DateTime: 2022/6/30 10:17
 **/
@Component
@Slf4j
public class MailReceiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(MailReceiver.class);

    @Resource
    private JavaMailSenderImpl javaMailSender;

    @Autowired
    private MailProperties mailProperties;
    @Resource
    private TemplateEngine templateEngine;

    @RabbitListener(queues = "zangblog-queue")
    public void handler(CommentEmailInfo commentEmailInfo) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        try {
            //发件人
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            //收件人
            mimeMessageHelper.setTo(commentEmailInfo.getReceiveEmail());
            //主题
            mimeMessageHelper.setSubject("臧臧博客的福利");
            //发送日期
            mimeMessageHelper.setSentDate(new Date());
            //邮件发送内容
            Context context = new Context();
            context.setVariable("sendName", commentEmailInfo.getSendName());
            context.setVariable("receive", commentEmailInfo.getReceiveName());
            context.setVariable("msg", commentEmailInfo.getMsg());
            context.setVariable("article", commentEmailInfo.getArticleName());
            String mail = templateEngine.process("mail", context);
            mimeMessageHelper.setText(mail, true);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            LOGGER.error("邮件发送失败======>", e.getMessage());
        }
    }

    @RabbitListener(queues = "zang-queue")
    public void zhandler(CommentEmailInfo commentEmailInfo) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        try {
            //发件人
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            //收件人
            mimeMessageHelper.setTo(commentEmailInfo.getReceiveEmail());
            //主题
            mimeMessageHelper.setSubject("臧臧博客的福利");
            //发送日期
            mimeMessageHelper.setSentDate(new Date());
            //邮件发送内容
            Context context = new Context();
            context.setVariable("sendName", commentEmailInfo.getSendName());
            context.setVariable("receive", commentEmailInfo.getReceiveName());
            context.setVariable("msg", commentEmailInfo.getMsg());
            context.setVariable("article", commentEmailInfo.getArticleName());
            String mail = templateEngine.process("zang", context);
            mimeMessageHelper.setText(mail, true);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            LOGGER.error("邮件发送失败======>", e.getMessage());
        }
    }

    @RabbitListener(queues = "verificationCode-queue")
    public void verificationCodeHandler(VerificationCodeInfo VerificationCodeInfo) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        try {
            //发件人
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            //收件人
            mimeMessageHelper.setTo(VerificationCodeInfo.getEmail());
            //主题
            mimeMessageHelper.setSubject("臧臧博客的验证码");
            //发送日期
            mimeMessageHelper.setSentDate(new Date());
            //邮件发送内容
            Context context = new Context();
            context.setVariable("verificationCode", VerificationCodeInfo.getVerificationCode());

            String mail = templateEngine.process("verificationCode", context);
            mimeMessageHelper.setText(mail, true);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            LOGGER.error("邮件发送失败======>", e.getMessage());
        }
    }

}
